﻿using AiMachineControl.Common;
using AiMachineControl.IService;
using AiMachineControl.Service;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.Hosting;

class Program
{
    private static IConfiguration? _appConfiguration;

    static async Task Main(string[] args)
    {
        Console.Title = "AI Computing Machine Client";
        Console.WriteLine("The service has started and is waiting to execute the command...");
        await CreateHostBuilder(args).RunConsoleAsync();
        Console.ReadLine();
    }

    public static IHostBuilder CreateHostBuilder(string[] args) =>
           Host.CreateDefaultBuilder(args)
               .ConfigureAppConfiguration((hostContext, configApp) =>
               {
                   var hostingEnvironment = hostContext.HostingEnvironment;
                   configApp.AddJsonFile(path: "appsettings.json", optional: true, reloadOnChange: true); 
                   configApp.AddJsonFile(path: $"appsettings.{hostingEnvironment.EnvironmentName}.json", optional: true, reloadOnChange: true);
                   _appConfiguration = AppConfigurations.GetConfiguration(hostingEnvironment.ContentRootPath);
               })
               .ConfigureServices((hostContext, services) =>
               {
                   //注入配置文件读取
                   services.AddSingleton(_appConfiguration);
                   //注入访问本地文件所需的服务
                   services.AddSingleton<IFileProvider>(new PhysicalFileProvider(Directory.GetCurrentDirectory()));
                   //注入MyService到DI容器
                   services.AddSingleton<IAppsettingService, AppsettingService>();
                   services.AddSingleton<IFileService, FileService>();
                   services.AddSingleton<ICommandProcessService, CommandProcessService>();
                   services.AddSingleton<GlobalService>();
                   //注册后台定时服务
                   services.AddHostedService<HostedService>();
               });
}
